package com.sansan.qiangji.controller.system;

import com.sansan.qiangji.VO.system.menu.MenuPowerVO;
import com.sansan.qiangji.VO.system.menu.MenuVO;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.system.menu.MenuAddForm;
import com.sansan.qiangji.realize.system.MenuReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    private MenuReal menuReal;

    @Resource
    public void setMenuReal(MenuReal menuReal) {
        this.menuReal = menuReal;
    }

    @RequestMapping("/create")
    public ResultVO<Object> createMenu(@Valid @RequestBody MenuAddForm menuAddForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建菜单】参数不正确，MenuForm={}", menuAddForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        menuReal.MenuCreate(menuAddForm);
        return ResultVOUtil.success(null);
    }

    @GetMapping("/userMenuList")
    public ResultVO<Object> getUserMenus() {
        MenuVO menuVO = menuReal.userMenuList();
        return ResultVOUtil.success(menuVO);
    }

    @GetMapping("/list")
    public ResultVO<Object> getMenus() {
        MenuVO menuVO = menuReal.getMenus();
        return ResultVOUtil.success(menuVO);
    }

    @GetMapping("/userMenuPowerList")
    public ResultVO<Object> userMenuPowerList(@RequestParam(name = "userId") Integer userId) {
        MenuPowerVO menuPowerVO = menuReal.userMenuPowerList(userId);
        return ResultVOUtil.success(menuPowerVO);
    }

    @GetMapping("/roleMenuPowerList")
    public ResultVO<Object> roleMenuPowerList(@RequestParam(name = "roleId") Integer roleId) {
        MenuPowerVO menuPowerVO = menuReal.roleMenuPowerList(roleId);
        return ResultVOUtil.success(menuPowerVO);
    }

    @PostMapping("/changeStatus")
    public ResultVO<Object> changeMenuStatus(@RequestParam(name = "menuId") Integer menuId) {
        menuReal.changeMenuStatus(menuId);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/edit")
    public ResultVO<Object> editMenu(@RequestParam(name = "menuId") Integer menuId,
                                     @RequestParam(name = "menuName") String menuName,
                                     @RequestParam(name = "menuPath") String menuPath) {
        MenuVO menuVO = menuReal.editMenu(menuId, menuName, menuPath);
        return ResultVOUtil.success(menuVO);
    }

    @PostMapping("/delete")
    public ResultVO<Object> deleteMenu(@RequestParam(name = "menuId") Integer menuId) {
        menuReal.deleteMenu(menuId);
        return ResultVOUtil.success(null);
    }
}
